﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Collections;
using HardwareLibrary.PLCLibrary;
using HardwareLibrary.IDReaderLibrary;
using HardwareLibrary.FTPLibrary;
using HardwareLibrary.ComLibrary;
using HardwareLibrary.MeterReaderLibrary;
using MyExcelAddIn2003.Core;

namespace MyExcelAddIn2003.ComOPHelper
{
    [ComVisibleAttribute(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class OPHelper : IOPHelper
    {
        
        #region "PLC操作方法"
        /// <summary>
        /// 设置道闸开启关闭的PLC参数
        /// </summary>
        /// <param name="iPortNum">端口号</param>
        /// <param name="strParam">端口参数，例："9600,N,8,1"</param>
        /// <param name="openBarCMD">开启道闸命令</param>
        /// <param name="stopSendCMD">停止道闸命令</param>
        public void SetBar_PLCInfo(int iPortNum, string strParam, string openBarCMD, string stopSendCMD)
        {
            Utility.plcStruct.iPort = iPortNum;
            Utility.plcStruct.strComParam = strParam;
            Utility.plcStruct.OpenBarCMD = openBarCMD;
            Utility.plcStruct.StopSendCMD = stopSendCMD;
        }
        /// <summary>
        /// 发送起杆命令
        /// </summary>
        public void SendOpenBarCmdByPLC()
        {
            PLCHelper plc = new PLCHelper();
            plc.SendOpenBarCommand(Utility.plcStruct);
        }
        #endregion
        #region ID读卡器操作方法
        /// <summary>
        /// 设置ID读卡器参数变量
        /// </summary>
        /// <param name="iPortNum"></param>
        /// <param name="iBaudRate"></param>
        /// <param name="iBeginFlag"></param>
        /// <param name="iEndFlag"></param>
        /// <param name="iStrLen"></param>
        /// <param name="iReaderBeginSite"></param>
        /// <param name="iReaderIDLen"></param>
        /// <param name="iIDCardBeginSite"></param>
        /// <param name="iIDCardDataLen"></param>
        /// <param name="macName"></param>
        public void SetIDReaderInfo(int iPortNum, int iBaudRate, int iBeginFlag, int iEndFlag, int iStrLen, int iReaderBeginSite, int iReaderIDLen, int iIDCardBeginSite, int iIDCardDataLen, string macName)
        {
            Utility.idRead.iPort = iPortNum;
            Utility.idRead.iBaudRate = iBaudRate;
            Utility.idRead.iBeginFlag = iBeginFlag;
            Utility.idRead.iEndFlag = iEndFlag;
            Utility.idRead.strLen = iStrLen;
            Utility.idRead.BeginSite = iReaderBeginSite;
            Utility.idRead.IDReaderIDLen = iReaderIDLen;
            Utility.idRead.IDCardBeginSite = iIDCardBeginSite;
            Utility.idRead.IDCardDataLen = iIDCardDataLen;
            Utility.idRead.macName = macName;
        }
        /// <summary>
        /// 设置ID读卡器参数变量
        /// </summary>
        /// <param name="iPortNum"></param>
        /// <param name="strParam"></param>
        /// <param name="iBeginFlag"></param>
        /// <param name="iEndFlag"></param>
        /// <param name="iStrLen"></param>
        /// <param name="iReaderBeginSite"></param>
        /// <param name="iReaderIDLen"></param>
        /// <param name="iIDCardBeginSite"></param>
        /// <param name="iIDCardDataLen"></param>
        /// <param name="macName"></param>
        public void SetIDReaderInfoByParam(int iPortNum, string strParam, int iBeginFlag, int iEndFlag, int iStrLen, int iReaderBeginSite, int iReaderIDLen, int iIDCardBeginSite, int iIDCardDataLen, string macName)
        {
            Utility.idRead.iPort = iPortNum;
            Utility.idRead.strParam = strParam;
            Utility.idRead.iBeginFlag = iBeginFlag;
            Utility.idRead.iEndFlag = iEndFlag;
            Utility.idRead.strLen = iStrLen;
            Utility.idRead.BeginSite = iReaderBeginSite;
            Utility.idRead.IDReaderIDLen = iReaderIDLen;
            Utility.idRead.IDCardBeginSite = iIDCardBeginSite;
            Utility.idRead.IDCardDataLen = iIDCardDataLen;
            Utility.idRead.macName = macName;
        }
        #endregion
        #region 仪表操作方法
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iPortNum"></param>
        /// <param name="strParam"></param>
        /// <param name="iBeginFlag"></param>
        /// <param name="iEndFlag"></param>
        /// <param name="iStrLen"></param>
        /// <param name="iDataBeginFlag"></param>
        /// <param name="iDataLen"></param>
        /// <param name="iDenominatorLen"></param>
        /// <param name="order"></param>
        /// <param name="macName"></param>
        /// <returns></returns>
        public bool SetWTInfoByParam(int iPortNum, string strParam, int iBeginFlag, int iEndFlag, int iStrLen, int iDataBeginFlag, int iDataLen, int iDenominatorLen, string order, string macName)
        {
            try
            {
                Utility.wtInfo.iPortNum = iPortNum;
                Utility.wtInfo.strParam = strParam;
                Utility.wtInfo.iBeginFlag = iBeginFlag;
                Utility.wtInfo.iEndFlag = iEndFlag;
                Utility.wtInfo.strLen = iStrLen;
                Utility.wtInfo.DataBeginFlag = iDataBeginFlag;
                Utility.wtInfo.DataLen = iDataLen;
                Utility.wtInfo.DenominatorLen = iDenominatorLen;
                Utility.wtInfo.DataOrder = order;
                Utility.wtInfo.macName = macName;
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iPortNum"></param>
        /// <param name="iBaudRate"></param>
        /// <param name="iBeginFlag"></param>
        /// <param name="iEndFlag"></param>
        /// <param name="iStrLen"></param>
        /// <param name="iDataBeginFlag"></param>
        /// <param name="iDataLen"></param>
        /// <param name="iDenominatorLen"></param>
        /// <param name="order"></param>
        /// <param name="macName"></param>
        /// <returns></returns>
        public bool SetWTInfoByBaudRate(int iPortNum, int iBaudRate, int iBeginFlag, int iEndFlag, int iStrLen, int iDataBeginFlag, int iDataLen, int iDenominatorLen, string order, string macName)
        {
            try
            {
                Utility.wtInfo.iPortNum = iPortNum;
                Utility.wtInfo.iBaudRate = iBaudRate;
                Utility.wtInfo.iBeginFlag = iBeginFlag;
                Utility.wtInfo.iEndFlag = iEndFlag;
                Utility.wtInfo.strLen = iStrLen;
                Utility.wtInfo.DataBeginFlag = iDataBeginFlag;
                Utility.wtInfo.DataLen = iDataLen;
                Utility.wtInfo.DenominatorLen = iDenominatorLen;
                Utility.wtInfo.DataOrder = order;
                Utility.wtInfo.macName = macName;
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
        #region 计量窗体的操作接口
        public void ShowWeightForm()
        {
            WeightFrm frm = new WeightFrm();
            frm.Show();
        }
        public void SetSheetToMainFormByParam(string strKey)
        {
            try
            {
                Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[Utility.BillWorkSheet];
                if (WeightFrm.GetInstance != null)
                {
                    Utility.CreateBillVO();
                    foreach (DictionaryEntry de in Utility.htVO)
                    {
                        if (de.Value.ToString() == strKey)
                        {
                            Control[] c = WeightFrm.GetInstance.Controls.Find(de.Key.ToString(), true);
                            if (c[0] != null)
                            {
                                c[0].Text = ws.get_Range(strKey, Type.Missing).Text.ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void SetSheetToMainForm()
        {
            try
            {
                Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[Utility.BillWorkSheet];
                if (WeightFrm.GetInstance != null)
                {
                    Utility.CreateBillVO();
                    foreach (DictionaryEntry de in Utility.htVO)
                    {
                        Control[] c = WeightFrm.GetInstance.Controls.Find(de.Key.ToString(),true);
                        if (c[0] != null)
                        {
                            c[0].Text = ws.get_Range(de.Value.ToString(), Type.Missing).Text.ToString();
                        }
                    }
                    Utility.YMweight = Convert.ToDouble(ws.get_Range("仪毛重", Type.Missing).Text);
                    Utility.YPweight = Convert.ToDouble(ws.get_Range("仪皮重", Type.Missing).Text);
                    Utility.Bill_Time = ws.get_Range("系统日期", Type.Missing).Text.ToString();
                    //WeightFrm.GetInstance.numericUpDown_rate.Value = Convert.ToDecimal(ws.get_Range("压磅比例", Type.Missing).Text);
                }
                ws = null;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void SetAdjustRateType(bool isEnable, int iType)
        {
            Utility.IsEnable_AdjustRate = isEnable;
            if (iType == Utility.Rate)
            {
                Utility.AdjustRateType = Utility.Rate;
            }
            else
            {
                Utility.AdjustRateType = Utility.Num;
            }

        }
        public void ChangeBackGroundFlag(bool flag)
        {
            Utility.DataIsRuningBackgroud = flag;
        }
        #endregion
        #region FTP操作方法
        public void SetPicFTP(bool isEnable, string FTPUrl, string User, string Pwd, string ServerFolder, int Port)
        {
            Utility.IsEnable_FTP = isEnable;
            Utility.ftpInfo.IpAddr = FTPUrl;
            Utility.ftpInfo.User = User;
            Utility.ftpInfo.Pwd = Pwd;
            Utility.ftpInfo.RemotePath = ServerFolder;
            Utility.ftpInfo.Port = Port;
        }
        public void StartFTPfolderListening(string FTPAddr, string User, string Pwd, string ServerFolder,int port)
        {
            FtpHelper ftp = new FtpHelper();
            ftp.FtpFloderListenStart(FTPAddr, User, Pwd, ServerFolder, port);
        }
        //图片本地保存位置
        public void SetPicLocalFolderPath(string FolderPath)
        {
            Utility.LocalPicSaveFolder = FolderPath;
        }
        #endregion
        #region 语音播放接口
        public void PlayOkSound()
        {
            HardwareLibrary.SoundLibrary.WavHelper.PlayWTOKSound();
        }
        public void PlayReadCardOKSound()
        {
            HardwareLibrary.SoundLibrary.WavHelper.PlayReadCardOKSound();
        }
        public void PlayReadCardErrSound()
        {
            HardwareLibrary.SoundLibrary.WavHelper.PlayReadCardErrSound();
        }
        #endregion
    }
}
